package com.example.demo.controller;

import com.example.demo.dto.IndexImgDto;
import com.example.demo.dto.StatusDto;
import com.example.demo.model.Result;
import com.example.demo.service.IndexImgService;
import com.example.demo.utils.OssUtil;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;

@RestController
@RequestMapping("/index-img")
@AllArgsConstructor
public class IndexImgController {
    private OssUtil ossUtil;
    private IndexImgService indexImgService;

    /**
     * 管理员端查轮播图
     * @return
     */
    @GetMapping("/backend")
    public Result getAllIndexImgBackend(){
        return  Result.buildSuccess(indexImgService.getAllIndexImgBackend());
    }

    @PutMapping("/backend/{id}")
    public Result changerStatus(@PathVariable("id") Integer id, @RequestBody StatusDto statusDto){
        statusDto.setId(id);
        return indexImgService.changeStatus(statusDto);
    }

    @GetMapping("/backend/{id}")
    public Result getByIndexImgId(@PathVariable("id") Integer id){
        return indexImgService.getById(id);
    }


    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable("id") Integer id){
        return indexImgService.deleteById(id);
    }

    @PostMapping("/add")
    public Result addIndexImg(@RequestBody IndexImgDto indexImgDto){
        return indexImgService.addIndexImg(indexImgDto);
    }

    //上传文件
    @PostMapping("/backend/fileOss")
    @ResponseBody
    public Result uploadImg(@RequestParam("file") MultipartFile[] fileList) throws Exception {
        ArrayList<String> urlList = new ArrayList<>();
        for (MultipartFile multipartFile : fileList) {
            String url = ossUtil.uploadFileAvatar(multipartFile);
            urlList.add(url);
            System.out.println(url);
        }
        return Result.buildSuccess(urlList);
    }
}
